<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<div id="target">Target</div>
<div id="description">Test user gesture behavior during touch events.</div>

<script>

var cancelEvent = false;
var openedPopup = undefined;
function handler(e) {
    if (openedPopup !== undefined)
        assert_unreached("Handler invoked multiple times");

    var w = window.open("about:blank", "_blank");
    if (w) {
        w.close();
        openedPopup = true;
    } else {
        openedPopup = false;
    }

    if (cancelEvent)
        e.preventDefault;
}

var target = document.getElementById('target');

function testPopupOnEventDuring(eventType, expectPopup, operation) {
    openedPopup = undefined;
    target.addEventListener(eventType, handler);

    operation();

    assert_not_equals(openedPopup, undefined,
                      eventType + ' handler was not invoked');
    assert_equals(openedPopup, expectPopup);

    target.removeEventListener(eventType, handler);
}

if (window.testRunner) {
    testRunner.setPopupBlockingEnabled(true);
}

var rect = target.getBoundingClientRect();
var targetX = rect.left + rect.width / 2;
var targetY = rect.top + rect.height / 2;

test(() => {
  cancelEvent = false;
  eventSender.addTouchPoint(targetX, targetY);
  testPopupOnEventDuring('touchstart', false, function() { eventSender.touchStart(); });
  eventSender.updateTouchPoint(0, targetX + 10, targetY);
  testPopupOnEventDuring('touchmove', false, function() { eventSender.touchMove(); });
  eventSender.notifyStartOfTouchScroll();
  eventSender.releaseTouchPoint(0);
  testPopupOnEventDuring('touchend', false, function() { eventSender.touchEnd(); });
}, "During a scroll, no touch events are user gestures");


test(() => {
cancelEvent = true;
eventSender.addTouchPoint(targetX, targetY);
testPopupOnEventDuring('touchstart', false, function() { eventSender.touchStart(); });
eventSender.updateTouchPoint(0, targetX + 10, targetY);
testPopupOnEventDuring('touchmove', false, function() { eventSender.touchMove(); });
eventSender.releaseTouchPoint(0);
testPopupOnEventDuring('touchend', true, function() { eventSender.touchEnd(); });
}, "During a drag that isn't a scroll, only touchend is a user gesture");

</script>
